/*
 * Copyright (C) 2011 SANSOU SYSTEMS Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jp.co.sansou.android.atnd.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * <pre>
 * データベース作成処理クラスです。
 * </pre>
 *
 * @author SANSOU SYSTEMS Co.
 */
public class AtndDBOpenHelper extends SQLiteOpenHelper {

	/** 自クラスのインスタンス */
	private static AtndDBOpenHelper instance;

	/** データベース名の定義 */
	private static final String DATABASE_NAME = "ChibAndroidAtnd.db";

	/** データベースバージョンの定義 */
	private static final int DATABASE_VERSION = 1;

	/** MST_ATNDの定義 */
	private final String CREATE_MST_ATND = "create table MST_ATND ( "
			+ " event_id primary key integer unique not null "
			+ ", title text not null"
			+ ", catch text " + ", description text" + ", event_url text"
			+ ",  started_at text" + ", ended_at text" + ", url text"
			+ ", limit integer not null default 0" + ", address text"
			+ ", place text" + ", lat integer default 0"
			+ ", lon integer default 0"
			+ ", owner_id integer not null " + ", owner_nickname text not null"
			+ ", owner_twitter_id text not null"
			+ ", accepted integer not null" + ", waiting integer not null"
			+ ", updated_at text not null"
			+ ", default_cnt integer default 0 )";

	/** TRN_USERの定義 */
	private final String CREATE_TRN_USER = "create table TRN_USER ("
			+ "_id integer primary key autoincrement "
			+ ", event_id integer not null " + ", user_id integer "
			+ ", nickname text not null " + ", twitter_id text "
			+ ", twitter_img text " + ", status integer not null )";

	/** TRN_RECEPTIONの定義 */
	private final String CREATE_TRN_RECEPTION = "create table TRN_RECEPTION ("
			+ " _id integer primary key autoincrement "
			+ ", event_id integer not null " + ", user_id integer "
			+ ", nickname text not null " + ", twitter_id text "
			+ ", twitter_img text " + " ) ";

	/** MST_ATNDの削除文 */
	private final String DROP_MST_ATND = "drop table if exists MST_ATND";

	/** TRN_USERの削除文 */
	private final String DROP_TRN_USER = "drop table if exists TRN_USER";

	/** TRN_RECEPTIONの削除文 */
	private final String DROP_TRN_RECEPTION = "drop table if exists TRN_RECEPTION";

	/** コンストラクタの定義 */
	private AtndDBOpenHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	/**
	 * <pre>
	 * MyCalendarDBOpenHelperのインスタンスを取得します。
	 * シングルトン実装です。
	 * </pre>
	 *
	 * @param context
	 *            Contextオブジェクト
	 * @return MyCalendarDBOpenHelperオブジェクト
	 */
	public static AtndDBOpenHelper getInstance(Context context) {
		if (instance == null) {
			instance = new AtndDBOpenHelper(context);
		}
		return instance;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATE_MST_ATND);
		db.execSQL(CREATE_TRN_USER);
		db.execSQL(CREATE_TRN_RECEPTION);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL(DROP_MST_ATND);
		db.execSQL(DROP_TRN_USER);
		db.execSQL(DROP_TRN_RECEPTION);
		onCreate(db);
	}

}
